import API from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectActive:3,
    type:-1,
    refund_time:'',
    refund:'',
    selectTitle:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('exitType')) {
      console.log('存在')
      this.setData({
        selectActive:wx.getStorageSync('exitType')
      })
    }
  },
  getDay (e) {
    this.setData({
      refund_time:e.detail.value,
      selectTitle:"活动开始前"+e.detail.value+"天均可退款"
    })
    console.log(this.data.selectTitle)
  },
  select (e) {
    var index = e.currentTarget.dataset.type
    var refund = null
    if (index == 1) {
      this.setData({
        selectActive:1,
        selectTitle:'活动开始前均可退款'
      })
      refund = 1
    }
    if (index == 2) {
      this.setData({
        selectActive:2
      })
      refund = 1
    }
    if (index == 3) {
      this.setData({
        selectActive:3,
        selectTitle:'不支持退款'
      })
      refund =0
    }
    this.setData({
      type:index,
      refund:refund
    })
  
  },
  settingSubmit () {
    var title = this.data.selectTitle
    var type = this.data.selectActive
    console.log(type)
    wx.setStorageSync('selectTitle',title)
    wx.setStorageSync('exitType',type)
    var data = {
      productid:wx.getStorageSync('product_id'),
      refund: this.data.refund,
      refund_time: this.data.refund_time
    }
    if(this.data.type == 2) {
      var regex = /^\d{1,3}$/
      if (regex.test(this.data.refund_time)) {
        this.requerst(data) 
      } else {
        wx.showModal({
          title: '',
          content: '请输入正确的天数，并且只能是数字',
        })
        return
      }
      
    } else {

      this.requerst(data)
    }

   
  },
  requerst (data) {
    API.exitCostSet(data).then(res=>{
      if (res.code ==0) {
        wx.navigateBack({
          delta: 1
        })
      }
    })
  }
})